<?php

namespace app\api\controller;

use support\Request;
use app\service\AiService;
use think\facade\Db;
use app\ai\model\Template;
use app\ai\model\User;
use app\ai\model\BalanceLog;
use plugin\saiadmin\exception\ApiException;
use app\ai\model\TemplatePrice;
class TaskController
{
    /**
     * 用户下单
     */
    public function create(Request $request)
    {
       $data = [
        'template_id' => $request->post('template_id'),
        'original_img_url' => $request->post('original_img_url'),
        'original_mask_url' => $request->post('original_mask_url')??'',
        'articulation' => (string)$request->post('articulation'),
        'manual' => $request->post('manual')??2,
        'remarks'=>$request->post('remarks')??'',
       ];
       //检查参数
       if(empty($data['template_id']) || empty($data['original_img_url'])) {
        throw new ApiException('参数错误');
       }
       DB::startTrans();
       try {
        //根据清晰度扣钱
        $template = Template::where('id', $data['template_id'])->find();
        if (!$template) {
            throw new ApiException('模板不存在');
        }
        $articulation = TemplatePrice::where('template_id', $data['template_id'])->where('sort', $data['articulation'])->find();
        if (!$articulation) {
            throw new ApiException('清晰度不存在');
        }
        $user = User::find($request->userData['user_id']);
        $price = $articulation['price'];
        if($articulation['type'] == 1){
            //金币支付
            if($user->coin_balance < $price){
                throw new ApiException('金币不足');
            }
            //创建日志
            BalanceLog::create([
                'user_id'=>$request->userData['user_id'],
                'type'=>1,
                'price'=>$price,
                'before'=>$user->coin_balance,
                'after'=>$user->coin_balance - $price,
                'balance'=>$price,
                'remarks'=>$template['name'].$articulation['name'].'扣除金币',
            ]);
            $user->coin_balance = $user->coin_balance - $price;
            $user->save();
          
        } else{
            //钻石支付
            if($user->diamond_balance < $price){
                throw new ApiException('钻石不足');
            }
            //创建日志
            BalanceLog::create([
                'uid'=>$request->userData['user_id'],
                'type'=>2,
                'price'=>$price,
                'before'=>$user->diamond_balance,
                'after'=>$user->diamond_balance - $price,
                'balance'=>$price,
                'remarks'=>$template['name'].$articulation['name'].'扣除钻石',
            ]);
            $user->diamond_balance = $user->diamond_balance - $price;
            $user->save();
        }
            
           $result = AiService::createTask($request->userData['user_id'], $data);
           $submit = AiService::submitServe($result['task_no']);
           DB::commit();
           return json(['code'=>200,'msg'=>'success','data'=>$submit]);
       } catch (\Exception $e) {
           DB::rollback();
           return json(['code'=>400,'msg'=>$e->getMessage(),'data'=>[]]);
       }
    }
    

}
